<template>
  <div class="whole-container">
    <div class="signIn-header">
      <img src="../../assets/img/logo.png" alt="">
      <span>云顶书院学员管理系统</span>
    </div>
    <div class="signIn-container">
      <div class="signIn-card">
        <div class="signIn-title">普通用户登录</div>
        <div class="input-group">
          <el-form :model="userSignIn" status-icon :rules="rules" ref="userSignIn" label-width="100px" class="demo-ruleForm">
            <el-form-item prop="userEmail">
                <el-input v-model="userSignIn.userEmail" placeholder="请输入邮箱"></el-input>
            </el-form-item>
            <el-form-item prop="userPassword">
                <el-input v-model="userSignIn.userPassword" placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-button type="primary" @click="userRegister('userSignIn')">登录</el-button>
            <div class="forgetPass">忘记密码</div>
          </el-form>
        </div>

      </div>
    </div>
    <div class="signIn-footer">
        Copyright © 2021 云顶书院学员管理系统 All Rights Reserved · 京ICP备 520521250号
    </div>
  </div>
</template>
<script>
import config from '../../action/config.js';

export default {
  name:'signIn',

  data(){
    return{
      userSignIn:{
        userEmail:'',
        userPassword:'',
      },
      rules:{
        userEmail:[
          { required: true, message: '请输入邮箱地址!', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址!', trigger: ['blur', 'change'] }
        ],
        userPassword: [
          { required: true, message: '请输入密码！', trigger: 'blur'}
        ],
      }  
    };
  },
  methods:{
    userRegister(formName){
      let params={
        userEmail:this.userSignIn.userEmail,
        userPassword:this.userSignIn.userPassword
      };
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.$post(config.api.post.User.userSignIn,params)
            .then((response)=>{
              this.userSignIn = response.data;
              this.token = response.data.accessToken;
              if(response.code == 0){
                console.log("登录成功");
                this.$message({
                  message: '登录成功！',
                  type: 'success'
                });
                localStorage.setItem("token",response.data.accessToken,"userName",response.data.userName,"userId",response.data.userId);
                localStorage.setItem("userName",response.data.userName);
                localStorage.setItem("userId",response.data.userId);
                this.$router.push('/userHome');
              }else if(response.code == -1){
                console.log("登录失败");
                this.$message.error(response.message);
              }else if(response.code == null){
                console.log("登录失败");
                this.$message.error(response.message);
              }
            });
        }else {
          this.$message.error("信息输入有误或为空！");
          return false;
        }
      });
    }
  }
}
</script>
<style scoped>
  .whole-container{
    width: 100%;
    height: 722px;
    background-image: url('../../assets/img/a.jpg');
    background-size: cover;
  }
  .signIn-header{
    width: 100%;
    height: 100px;
  }
  .signIn-header img{
    width: 80px;
    height: width;
    margin-left: 3%;
    vertical-align: middle;
  }
  .signIn-header span{
    font-size:18px;
    line-height: 100px;
    font-weight: 500;
    color:#fff;
    margin-left: 1%;
  }
  .signIn-container{
    width: 34%;
    height: 460px;
    margin: 0 auto;
    padding-top: 30px;
    border-radius: 36px;
    background-image: url('../../assets/img/background.jpg');
    box-shadow: 6px 6px 16px #000;
  }
  .signIn-card{
    width: 80%;
    height: 410px;
    margin: 0 auto;
    border-radius: 8px;
    background-color: #a1caf0;
    box-shadow: 4px 4px 6px rgba(19, 18, 18, 0.67);
  }
  .signIn-title{
    width: 100%;
    height: 50px;
    line-height: 50px;
    text-align: center;
    color: #4f5053;
    font-size: 26px;
    font-weight: 500;
    padding-top: 30px;
  }
  .el-form-item__content{
    margin-left: 18% !important;
  }
  .el-input{
    width: 78%;
  }
  .el-input__inner{
    height: 46px;
  }
  .el-input.is-active .el-input__inner, .el-input__inner:focus{
    border-color: #9f8e73 !important;
  }
  .input-group{
    margin-top: 40px;
  }
  /* 按钮 */
  .el-button--primary{
    width: 59%;
    height: 46px;
    margin-top: 24px;
    margin-left: 24%;
    background-color: #1d0d3b;
    border-color: #8b4646;
    font-size: 16px;
  }
  .el-button--primary:hover{
    background-color: #72b3e9b3;
    border-color: #8b4646b3;
  }
  .el-button--primary.is-active, .el-button--primary:active{
    background-color: #0f3e7c;
    border-color: #8b4646;
  }
  .el-button--primary.is-active, .el-button--primary:focus{
    background-color: #8160b6;
    border-color: #8b4646;
  }
  .forgetPass{
    width: 100%;
    padding-left: 69%;
    font-size: 13px;
    color: #606266ba;
    margin-top: 10px;
    cursor: pointer;
  }
  .signIn-footer{
    width: 100%;
    height: 30px;
    color: #fff;
    text-align: center;
    line-height: 30px;
    margin-top: 90px;
  }
</style>